﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MKAdminCore.ToolKit.StringHelper
{
    /// <summary>
    /// sql分页方法
    /// </summary>
    public static class SqlPagingHelper
    {
        /// <summary>
        /// 通用生成sql分页方法
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sortingSql"></param>
        /// <returns></returns>
        public static string GetPagingSql(string sql, int pageIndex, int pageSize, string sortingSql)
        {
            int pageLowerIndex = (pageIndex - 1) * pageSize;

            var resultSql = $@"{sql} {sortingSql}
                           OFFSET {pageLowerIndex} Rows    
                           FETCH NEXT {pageSize} ROWS ONLY;";

            return resultSql;
        }

        /// <summary>
        /// 通用生成sql分页总记录数
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sortingSql"></param>
        /// <returns></returns>
        public static string GetPagingCountSql(string sql)
        {
            var resultSql = $@"Select Count(1) From ({sql})A";

            return resultSql;
        }
    }
}
